#include "login_user_name.hpp"
#include <shadow.h>
#include "tlv_type.h"
#include <iostream>

using std::cout;
using std::endl;

namespace rocket 
{

login_user_name::login_user_name(shared_ptr<rocket::tcp_link>link,rocket::packet data)
:_link(link)
,_data(data)
{

}
login_user_name::~login_user_name()
{

}

void login_user_name::something()
{
//是登录发送用户名报文
	cout << "type=LOGIN_USER" << endl;
    //根据用户名获取密文盐值等相关内容
	spwd *pna = getspnam(_data.get_data().c_str());
	//从字符串中分离出盐值
	if (pna != NULL) 
    {
	    int salt_length = 0;
	    for (int i = 0, j = 0; i < strlen(pna->sp_pwdp); ++i) 
        {
		    if (pna->sp_pwdp[i] == '$') 
            {
			    ++j;
			    if (j == 4) 
                {
				    salt_length = i;
				    break;
			    }
		    }
	    }
        char salt[1024] = { 0 };
	    strncpy(salt, pna->sp_pwdp, salt_length);
        //将发来的用户名存储在链接对象中
        _link->set_user(_data.get_data());
	    rocket::tlv salt_tlv(rocket::LOGIN_SALT, strlen(salt),salt);
        //发送盐值给客户端
	    _link->add_send_task(salt_tlv);
	    cout << "send_salt_tlv : ";
	    salt_tlv.print();
    }

}
}
